package com.gof.example.state.gate;

/**
 * Created with IntelliJ IDEA.
 * Created by zhiyuan on 2018/5/13 17:05.
 * haiming.fei@tongdun.cn
 */
public class Gate {
    private State currentState, openState, closedState, openingState, stayOpenState, closingState;

    public Gate() {
        openState = new OpenState();
        closedState = new ClosedState();
        openingState = new OpeningState();
        stayOpenState = new StayOpenState();
        closingState = new ClosingState();
        currentState = openState;
    }

    private void setState(State s) {
        this.currentState = s;
    }

    public void click() {
        if (currentState == openState) {
            setState(stayOpenState);
        } else if (currentState == closedState) {
            setState(openingState);
        } else if (currentState == openingState) {
            setState(closingState);
        } else if (currentState == closingState) {
            setState(openingState);
        } else if (currentState == stayOpenState) {
            setState(closingState);
        }

        currentState.display();
    }

    public void complete() {
        if (currentState == openingState) {
            setState(openState);
        } else if (currentState == closingState) {
            setState(closedState);
        }

        currentState.display();
    }

    public void timeout() {
        if (currentState == openState) {
            setState(closingState);
        }
        currentState.display();
    }
}
